package cn.jjxx.modules.sys.service.impl;

import java.util.List;
import cn.jjxx.core.common.service.impl.CommonServiceImpl;
import cn.jjxx.core.query.wrapper.EntityWrapper;
import cn.jjxx.core.utils.ObjectUtils;
import cn.jjxx.core.utils.StringUtils;
import cn.jjxx.modules.activiti.workflow.BillBean;
import cn.jjxx.modules.sys.mapper.BillActConfigMapper;
import cn.jjxx.modules.sys.entity.BillActConfig;
import cn.jjxx.modules.sys.service.IBillActConfigService;

import org.framework.superutil.j2se.ClassUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.annotations.TableName;

/**   
 * @Title: 系统流程单据信息配置
 * @Description: 系统流程单据信息配置
 * @author zcg
 * @date 2018-08-27 17:29:43
 * @version V1.0   
 *
 */
@Transactional
@Service("billActConfigService")
public class BillActConfigServiceImpl  extends CommonServiceImpl<BillActConfigMapper,BillActConfig> implements  IBillActConfigService {

	private static final String LM = "LM";
	
	private static final String LEFT_HKH = "{";
	
	private static final String RIGHT_HKH = "}";
	
	private static final String ID = "id";
	
	private static final String LEFT_QUOTE_MARK = "'";
	
	private static final String RIGHT_QUOTE_MARK = "'";
	
	public static final String BU_LIMIT = "BXS";//不限时
	
	public static final String YEAR_YEAR = "YY";//年-年（时间限制）
	
	public static final String MONTH_MONTH = "MM";//月-月（时间限制）
	
	public static final String DAY_DAY = "DD";//天-天（时间限制）
	
	public static final String MONTHDAY_MONTHDAY = "MDMD";//月天-月天（时间限制）
	
	@Override
	public boolean insertBatch(List<BillActConfig> list) {
		if(list!=null&&list.size()>0){
			return baseMapper.insertBatch(list);
		}
		return true;
	}

	@Override
	public boolean updateBatch(List<BillActConfig> list) {
		if(list!=null&&list.size()>0){
			return baseMapper.updateBatch(list);
		}
		return true;
	}

	@Override
	public BillBean getBillBeanConfig(String menuId, BillBean billBean) {
		EntityWrapper<BillActConfig> wrapper = new EntityWrapper<BillActConfig>(BillActConfig.class);
		billBean.setCallBack(false);
		wrapper.eq("menuId", menuId);
		BillActConfig config = this.selectOne(wrapper);
		if(!ObjectUtils.isNullOrEmpty(config)){
			String type = config.getType();
			String formUrl = config.getFormUrl();
			formUrl = formUrl.replace(LEFT_HKH+ID+RIGHT_HKH, billBean.getBillId());
			String appFormUrl = config.getAppFormUrl();
			appFormUrl = StringUtils.isNotEmpty(appFormUrl)?appFormUrl.replace(LEFT_HKH+ID+RIGHT_HKH, billBean.getBillId()):null;
			billBean.setConfigId(config.getId());
			billBean.setNoticeTemplet(config.getNoticeTemplet());
			billBean.setTaskAuditeUrl(config.getTaskAuditeUrl());
			billBean.setTaskSignUrl(config.getTaskSignUrl());
			billBean.setBillName(config.getBillName());
			billBean.setFormUrl(formUrl);
			billBean.setAppFormUrl(appFormUrl);
			billBean.setStatusFeild(config.getStatusFeild());
			billBean.setIdFeild(config.getIdFeild());
			billBean.setId(config.getId());
			//补充字段
			String customSql = config.getCustomSql();
			customSql = customSql.replace(LEFT_HKH+config.getIdFeild()+RIGHT_HKH, LEFT_QUOTE_MARK+billBean.getBillId()+RIGHT_QUOTE_MARK);
			billBean.setCustomSql(customSql);
			billBean.setFormSaveUrl(config.getFormSaveUrl());
			billBean.setLimitType(config.getLimitType());
			billBean.setLimitSubmitStartDate(config.getLimitSubmitStartDate());
			billBean.setLimitSubmitEndDate(config.getLimitSubmitEndDate());
			billBean.setAutoSubmit(config.getIsAutoSubmit());
			billBean.setTableName(config.getTableName());
			billBean.setCallBack(config.getIsCallBack().equals(String.valueOf(1))?true:false);
			if(billBean.isCallBack()){
				billBean.setCallClass(config.getCallClass());
				billBean.setCallMethod(config.getCallMethod());
			}
			if(type.equals(LM)){
				Class<?> cls = ClassUtil.getEntityClass(config.getClassName());
				TableName table = cls.getAnnotation(TableName.class);
				String tableName = table.value();
				billBean.setTableName(tableName);
			}
		}
		return billBean;
	}
}
